import {
  uploadByNomarl,
  uploadByPieces,
  getCurrentUploadFileList,
  saveCurrentUploadFileList
} from './uploadByBreakPoint.js'

export default {
  data () {
    return {
      fileList: [],
      pcSelectedFileList: []
    }
  },

  watch: {
    $route () {
      localStorage.removeItem('localStorage_file_breakpoint_uploading')
    }
  },

  created () {
    if (window.$app.appType === 'pc') {
      window.$twh5.basic.uploadSelectFileCompleteByMd5 = this.uploadSelectFileCompleteByMd5
    }
  },

  methods: {
    // 文件支持断点续传
    async uploadFileByBreakPoint (options, useByNetDisk = false) {
      let { data, file } = options
      console.log('options:', options)
      if (window.$app.appType === 'pc' && !window.$ui.isEmptyValue(this.pcSelectedFileList)) {
        this.pcSelectedFileList.map(pcFile => {
          if (pcFile.FileName === file.name) {
            data.Md5Code = pcFile.Md5Code
          }
        })
      }
      saveCurrentUploadFileList(file.name)
      let uploadingFiles = getCurrentUploadFileList()
      // console.log('uploadFileByBreakPoint uploadingFiles:', uploadingFiles)
      window.$twFrontLog.send({
        content: `获取当前正在上传中的文件: ${JSON.stringify(uploadingFiles)}`,
        moduleName: 'saveCurrentUploadFileList',
        linesOfCode: 0,
        templeName: 'uploadByBreakPoint-mixins.js'
      })
      const minBreakPointFileSize =
        parseInt(
          window.$app.platConfig['paas_platform_file_upload_file_slice_size']
        ) || 20 * 1024 * 1024
      // const minBreakPointFileSize = 2 * 1024 * 1024
      const CancelToken = this.$axios.CancelToken
      const source = CancelToken.source()
      const fileMd5Loading = window.$ui.checkUserIsTeacher() && file.size > 100 * 1024 * 1024
      file.axiosSource = source
      if (file.size < minBreakPointFileSize) {
        // || uploadingFiles.length > 2
        window.$twFrontLog.send({
          content: `走正常FS Upload V2接口 当前的文件大小不再分片范围: ${JSON.stringify(file.size < minBreakPointFileSize)}`,
          moduleName: 'uploadFileByBreakPoint',
          linesOfCode: 0,
          templeName: 'uploadByBreakPoint-mixins.js'
        })
        let url = data.useByNetDisk ? window.$app.netDiskFileUploadUrl : window.$app.fileUploadUrl
        const res = await uploadByNomarl(
          url,
          file,
          this.uploadData,
          this.uploadHeaders,
          options
        )
        return res.data
      } else {
        let url = window.$app.gateway + (data.useByNetDisk ? '/fs/chunk/cloud-disk/create' : '/fs/chunk/create')
        data.file = file
        const res = await uploadByPieces(url, data, this.uploadData, options, fileMd5Loading)
        return res
      }
    },

    // 删除文件并取消上传
    uploadFileBeforeRemove (file, fileList) {
      if (file.raw && file.raw.axiosSource) {
        file.raw.axiosSource.cancel('用户取消上传文件')
      }
      if (this.fileList.indexOf(file) !== -1) {
        this.fileList.splice(this.fileList.indexOf(file), 1)
      }
      window.$twFrontLog.send({
        content: `用户手动删除了文件, 取消分片上传同时删除列表`,
        moduleName: 'uploadFileBeforeRemove',
        linesOfCode: 0,
        templeName: 'uploadByBreakPoint-mixins.js'
      })
    },

    // PC拦截文件选择器回调文件MD5
    uploadSelectFileCompleteByMd5 (data) {
      this.pcSelectedFileList = data.SelectFileInfoList || []
    }
  }
}
